﻿package com.sms.monitor.ui.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.sms.monitor.R;
import com.sms.monitor.constant.Constant;
import com.sms.monitor.utils.SharedPreferencesUtil;
import com.sms.monitor.utils.Util;

/**
 * 关键字设置界面。
 * 
 *
 */
public class KeywordSettingActivity extends Activity implements OnClickListener,OnItemLongClickListener {
	private ListView				mKeywords;
	private Button					mAddKeyword;
	private EditText				mKeyword;
	private List<String>			mKeywordList	= new ArrayList<String>();
	private ArrayAdapter<String>	mKeywordAdapter;
	private String					mKeywordStr;
	private Set<String>				mKeywordSet;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.configration_keyword_layout);
		findViews();
	}

	private void findViews() {
		// 
		mKeywords = (ListView) findViewById(R.id.lv_keywords);
		mAddKeyword = (Button) findViewById(R.id.btn_add_keyword);
		mKeyword = (EditText) findViewById(R.id.et_keyword);
		mKeywordStr = SharedPreferencesUtil.getValue(this, Constant.KEY_SMS_SPF_KEYWORD);
		
		mKeywordSet =  Util.converStringToSet(mKeywordStr);
		
		//设置界面显示。
		SharedPreferencesUtil.putValue(this, Constant.KEY_SMS_SPF_KEYWORD, Util.converSetToString(mKeywordSet));
		mKeywordStr = SharedPreferencesUtil.getValue(this, Constant.KEY_SMS_SPF_KEYWORD);
		mKeywordSet = Util.converStringToSet(mKeywordStr);
		mKeywordList.addAll(mKeywordSet);
		mKeywordAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mKeywordList);
		mKeywords.setAdapter(mKeywordAdapter);
		mAddKeyword.setOnClickListener(this);
		mKeywords.setOnItemLongClickListener(this);
	}

	/**
	 * 处理点击事件，添加关键字
	 */
	@Override
	public void onClick(View v) {
		// 
		switch (v.getId()) {
		case R.id.btn_add_keyword:
			String phone = mKeyword.getText().toString().trim();
			if (!TextUtils.isEmpty(phone)) {
				mKeywordSet.add(phone);
				SharedPreferencesUtil.putValue(this, Constant.KEY_SMS_SPF_KEYWORD, Util.converSetToString(mKeywordSet));
				mKeywordList.clear();
				mKeywordList.addAll(mKeywordSet);
				mKeywordAdapter.notifyDataSetChanged();
				mKeyword.setText("");
			}
			break;

		default:
			break;
		}
	}
	
	/**
	 * 长按删除关键字。
	 */
	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1,final int position, long arg3) {
		// 
		AlertDialog.Builder  builder = new AlertDialog.Builder(this);
		builder.setTitle(getString(R.string.notice)).setMessage(getString(R.string.delete_or_not)).setNegativeButton(getString(R.string.delete), new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// 
				mKeywordSet.remove(mKeywordList.get(position));
				SharedPreferencesUtil.putValue(KeywordSettingActivity.this, Constant.KEY_SMS_SPF_KEYWORD, Util.converSetToString(mKeywordSet));
				mKeywordList.clear();
				mKeywordList.addAll(mKeywordSet);
				mKeywordAdapter.notifyDataSetChanged();
				mKeyword.setText("");
				dialog.cancel();
				
			}
		}).setPositiveButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// 
				dialog.cancel();
			}
		}).show();
		
		return false;
	}

}
